/*
  S.M.A.C.K - An operating system kernel
  Copyright (C) 2010,2011 Mattias Holm and Kristian Rietveld
  For licensing and a full list of authors of the kernel, see the files
  COPYING and AUTHORS.
*/
#include <device.h>
#include <assert.h>

dev_t* devices[MAX_DEVICES];

void ramdisk_init(void);
void video_init(void);

void
dev_init(void)
{
  /* NOTE: serial_init() is called by boot.c, since this driver needs
   * to be initialized immediately after MMU switch on.  This is because
   * of the hack we use to allow access to the serial driver before
   * MMU initialization.
   */
  ramdisk_init();
  video_init();
}


void
dev_register(dev_major_t major, dev_t *dev)
{
  dev->major = major;
  devices[major] = dev;
}

dev_t*
dev_find(dev_major_t major)
{
  assert(major < MAX_DEVICES && "out of bounds");
  return devices[major];
}

size_t
dev_read(dev_t *dev, dev_minor_t minor, void *buff, size_t nbytes)
{
  if (!dev->ops->read)
    return -1;

  return dev->ops->read(dev, minor, buff, nbytes);
}

size_t
dev_write(dev_t *dev, dev_minor_t minor, const void *buff, size_t nbytes)
{
  if (!dev->ops->write)
    return -1;

  return dev->ops->write(dev, minor, buff, nbytes);
}

off_t
dev_seek(dev_t *dev, dev_minor_t minor, off_t offset, int whence)
{
  if (!dev->ops->seek)
    return -1;

  return dev->ops->seek(dev, minor, offset, whence);
}

int
dev_open(dev_t *dev, dev_minor_t minor)
{
  if (!dev->ops->open)
    return -1;

  return dev->ops->open(dev, minor);
}

int
dev_close(dev_t *dev, dev_minor_t minor)
{
  if (!dev->ops->close)
    return -1;

  return dev->ops->close(dev, minor);
}

int
dev_ioctl(dev_t *dev, dev_minor_t minor, uint32_t request, uint32_t param)
{
  if (!dev->ops->ioctl)
    return -1;

  return dev->ops->ioctl(dev, minor, request, param);
}
